#include "PowerUp.h"
#include "../SGD Wrappers/DestroyEntityMessage.h"
#include "../CreateGoalieMessage.h" 
#include "Player.h"
#include "../particles/Emitter.h"

CPowerUp::CPowerUp()
{
	pEffect = CParticleManager::GetInstance()->CreateEmitter(&m_ptPosition, "Resources/Particles/Magic.xml", CEmitter::STEADY);
}


CPowerUp::~CPowerUp()
{
	if (pEffect != nullptr)
	{
		CParticleManager::GetInstance()->DeleteEmitter(pEffect);
		pEffect = nullptr;
	}
}

void CPowerUp::Update(float elapsedTime) /*override*/
{
	m_fDespawnTimer -= elapsedTime;

	if (m_fDespawnTimer <= 0 && m_bIsHeld == false)
	{
		//this->currAnimation.SetCurrFrame(0);
		//this->currAnimation.SetPlaying(false);
		//this->currAnimation.SetFinished(true);
		//this->currAnimation.SetLooping(false);
	
		DestroyEntityMessage *d = new DestroyEntityMessage{ this };
		d->QueueMessage();
		d = nullptr;
	}

	if (m_bIsHeld == true)
	{
		m_fHeldTimer -= elapsedTime;
		//this->currAnimation.SetCurrFrame(0);
		//this->currAnimation.SetPlaying(false);
		//this->currAnimation.SetFinished(true);
		//this->currAnimation.SetLooping(false);
		if (m_fHeldTimer >= 0)
		{
			Effect();

			//Additional ability meter power up
			//will still get deleted.
			DestroyEntityMessage *d = new DestroyEntityMessage{ this };
			d->QueueMessage();
			d = nullptr;
		}
	}
}

void CPowerUp::SetOwner(Entity* o)
{
	if (o != nullptr)
		o->Release();

	owner = o;

	if (o != nullptr)
		owner->AddRef();
}

void CPowerUp::Effect()
{
	//enum PUpType{
	//	PUP_GOALIE, PUP_NOAGGRO, PUP_POISION, PUP_ABILITYCHARGE,
	//	PUP_KNOCKBACK, PUP_GOALSIZE, PUP_TRAPCHANGE, PUP_SLOWPUSLE,
	//	PUP_STEAL
	//};
	switch (m_eType)
	{
	case PUP_GOALIE:
	{
					   CreateGoalieMessage* pMsg = new CreateGoalieMessage(owner);
					   pMsg->QueueMessage();
	}
		break;
	case PUP_NOAGGRO:
	{
						SGD::Event* pEvent = new SGD::Event("NO_AGGRO", nullptr, this);
						pEvent->QueueEvent();
						m_bIsHeld = false;
	}
		break;
	case PUP_POISON:
		break;
	case PUP_ABILITYCHARGE:
		break;
	case PUP_KNOCKBACK:
		dynamic_cast<CPlayer*>(owner)->SetKnockBack(dynamic_cast<CPlayer*>(owner)->GetKnockBack() - 20);
		break;
	case PUP_GOALSIZE:
		break;
	case PUP_TRAPCHANGE:
		break;
	case PUP_SLOWPULSE:
		break;
	case PUP_STEAL:
		dynamic_cast<CPlayer*>(owner)->SetStealChance(15);
		break;
	}
}